<?php
/**
 * Created by PhpStorm.
 * User: Lzhy ysjywz@gmail.com
 * Date: 2018/6/19
 * Time: 上午9:59
 */
namespace Core\common;

class Cache
{
    /**
     * 取出缓存
     * @param $key
     * @return array|bool|\Memcache|mixed|null|\Redis|string
     */
    public static function get($key)
    {
        if($key){
            return self::_eval($key);
        }
    }

    /**
     * 设置缓存
     * @param $key
     * @param $value
     * @param null $ttl
     * @return array|bool|\Memcache|mixed|null|\Redis|string
     */
    public static function set($key,$value,$ttl = CACHE_EXPIRE_LONG)
    {
        if($key){
            return cache_write($key,$value,$ttl);
        }
    }

    /**
     * 删除缓存
     * @param $key
     * @return array|bool|\Memcache|\Redis
     */
    public static function del($key)
    {
        if($key){
            return cache_delete($key);
        }
    }

    /**
     * 清空全部缓存
     * @return array|bool|\Memcache|\Redis
     */
    public static function clean()
    {
        return cache_clean();
    }

    /**
     * @param $key
     * @return array|bool|\Memcache|mixed|\Redis|string
     */
    private static function _eval($key)
    {
        $keyArr = explode('.',$key);
        $value = cache_load(array_shift($keyArr));
        if($value && $keyArr){
            foreach ($keyArr as $v){
                $value = $value[$v];
            }
        }

        return $value;
    }
}